/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ErrorDialog.java
 *
 * Created on 17-Feb-2010, 11:26:22
 */

package TimeRecording.forms;

import java.awt.Toolkit;
import java.awt.Window;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author dpatchett
 */
public class ErrorDialog extends javax.swing.JDialog {

    private final static Logger LOGGER = Logger.getLogger(
            ErrorDialog.class.getName());

    public static void showError(Window parent, String errorMessage) {
        ErrorDialog dlg = new ErrorDialog(parent, errorMessage);
        dlg.setVisible(true);
    }

    public static void showError(Window parent, Exception exception) {
        StringWriter errorMessage = new StringWriter();
        exception.printStackTrace(new PrintWriter(errorMessage));
        ErrorDialog dlg = new ErrorDialog(parent, errorMessage.toString());
        dlg.setVisible(true);
    }

    /** Creates new form ErrorDialog */
    public ErrorDialog(Window parent, String errorMessage) {
        super(parent, ModalityType.APPLICATION_MODAL);
        initComponents();
        errorMessageArea.setText(errorMessage);
        setSize(1000, 600);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        errorMessagePanel = new javax.swing.JScrollPane();
        errorMessageArea = new javax.swing.JTextArea();
        buttonPanel = new javax.swing.JPanel();
        copyButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        errorMessagePanel.setName("errorMessagePanel"); // NOI18N

        errorMessageArea.setColumns(20);
        errorMessageArea.setEditable(false);
        errorMessageArea.setRows(5);
        errorMessageArea.setName("errorMessageArea"); // NOI18N
        errorMessagePanel.setViewportView(errorMessageArea);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 187;
        gridBagConstraints.ipady = 221;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 8, 3, 8);
        getContentPane().add(errorMessagePanel, gridBagConstraints);

        buttonPanel.setName("buttonPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(ErrorDialog.class);
        copyButton.setText(resourceMap.getString("copyButton.text")); // NOI18N
        copyButton.setName("copyButton"); // NOI18N
        copyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(copyButton);

        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(saveButton);

        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(closeButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 3, 0);
        getContentPane().add(buttonPanel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void copyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyButtonActionPerformed

        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(new StringSelection(errorMessageArea.getText()), null);
    }//GEN-LAST:event_copyButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        JFileChooser fileChooser = new JFileChooser();

        if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {

            File errorFile = fileChooser.getSelectedFile();

            try {
                FileWriter errorWriter = new FileWriter(errorFile);
                errorWriter.write(errorMessageArea.getText());
                errorWriter.close();

            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Error saving error log");
                LOGGER.log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton copyButton;
    private javax.swing.JTextArea errorMessageArea;
    private javax.swing.JScrollPane errorMessagePanel;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables
}
